package com.footprint.common.httpclient;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;

import com.footprint.common.util.Constants;
import com.footprint.common.util.StringUtils;

public class BasicHttpRequest implements HttpRequestInterface {
	private String hostname;
	private String protocol;
	private Constants.HttpMethod httpMethod;
	private String path;
	private String action;
	private URI uri;
	private HttpUriRequest uriRequest;
	
	protected List<NameValuePair> paramList;
	
	
	public BasicHttpRequest( String host, String protocol, String path, String action, Constants.HttpMethod httpMethod ){
		if( StringUtils.isEmpty(host) || StringUtils.isEmpty(path) ){
			throw new IllegalArgumentException( "Hostname and path must be declared" );
		}
		this.hostname = host;
		this.protocol = ( protocol == null ) ? "http" : protocol;
		this.path = path;
		this.action = action;
		this.httpMethod = httpMethod;
		this.paramList = new ArrayList<NameValuePair>();
		// this.initialize();
	}

	public String getHostname() {
		return hostname;
	}

	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	public String getProtocol() {
		return protocol;
	}

	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	/* (non-Javadoc)
	 * @see com.footprint.common.httpclient.HttpRequestInterface#getHttpMethod()
	 */
	public Constants.HttpMethod getHttpMethod() {
		return httpMethod;
	}

	public void setHttpMethod(Constants.HttpMethod httpMethod) {
		this.httpMethod = httpMethod;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	protected void initialize() {
		// TODO Auto-generated method stub
		abort();
		if( StringUtils.isNotEmpty(hostname) && StringUtils.isNotEmpty(path) ){
			try {
				uri = buildUrl();
			} catch (URISyntaxException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.uriRequest = Constants.initializeHttpRequest(uri, getHttpMethod());
	}
	
	public void abort(){
		if( this.uriRequest != null ){
			this.uriRequest.abort();
		}
	}
	
	
	private URI buildUrl() throws URISyntaxException {
		// TODO Auto-generated method stub
		
		return URIUtils.createURI(protocol, hostname, -1, path + ( StringUtils.isEmpty(action) ? "" : action ), URLEncodedUtils.format(paramList, "UTF-8"), null);
	}

	public void addUrlParams( NameValuePair param ){
		this.paramList.add( param );
	}


	/* (non-Javadoc)
	 * @see com.footprint.common.httpclient.HttpRequestInterface#getUri()
	 */
	public URI getUri() {
		return uri;
	}


	@Override
	public HttpUriRequest getUriRequest() {
		// TODO Auto-generated method stub
		return this.uriRequest;
	}
}
